<?php

/**
 * parser
 *
 * @author Admin
 */
class Parser_News extends Model_Abstract
{
    protected $_table_name = 'parsed_news';

	public function check( $title )
	{
		$fields = array(
			'title' => $title,
		);

		$fields = $this->_paramsToString($fields);

		$sql = "SELECT * FROM " . $this->_table_name . " WHERE " . $fields;

		$rows = $this->_db->get_results( $sql );
		
		return $rows;
	}

	public function getNewsForParsing( $date = false, $limit = false )
	{
		if( !$date )
			$date = date('Y-m-d', time());
		
		$fields = array(
			'next_parsing' => $date,
		);

		$fields = $this->_paramsToString($fields);

		$sql = "SELECT * FROM " . $this->_table_name . " WHERE " . $fields . " " . ( $limit ? "LIMIT " . $limit : "" );

		$rows = $this->_db->get_results( $sql );
		
		return $rows;
	}

	public function insert( $parser_id, $item_id, $title, $url, $next_parsing, $news_date = false )
    {
		$fields = array(
			'parser_id' => $parser_id,
			'item_id' => $item_id,
			'title' => $title,
			'url' => $url,
			'add_date' => date('Y-m-d H:m:s', time()),
			'next_parsing' => $next_parsing,
			'news_date' => $news_date
		);

		$fields = $this->_paramsToString($fields);

		$sql = "INSERT INTO " . $this->_table_name . " SET " . $fields;

		$this->_db->get_results($sql);

		return $this->_getLastInsertId();
    }
    
    public function update( $id, $parser_id = false, $item_id = false, $title = false, $url = false, $next_parsing = false, $news_date = false )
    {
		$fields = array(
			'parser_id' => $parser_id,
			'item_id' => $item_id,
			'title' => $title,
			'url' => $url,
			'next_parsing' => $next_parsing,
			'news_date' => $news_date
		);

		$fields = $this->_paramsToString($fields);

		$sql = "UPDATE " . $this->_table_name . " SET " . $fields . " WHERE id = $id";

		return $this->_db->get_results($sql);
    }
    
    
}